<template>
	<!-- 添加房屋 -->
	<view class="building-add">
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="avator==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="avator"></image>
			</view>
		</view>
		<view class="add-content">
			<view class="add-item">
				<view class="item-left">名称<text>*</text></view>
				<view class="item-right"><input placeholder="请输入名称" placeholder-style="color:#999;font-size:26rpx" v-model="name"/></view>
			</view>
			<view class="add-item">
				<view class="item-left">房屋注册号<text>*</text></view>
				<view class="item-right"><input placeholder="请输入房屋注册号" type="number" placeholder-style="color:#999;font-size:26rpx" v-model="houseRegisterCode"/></view>
			</view>
			<view class="add-item" @click="dictListShow=true">
				<view class="item-left">房屋类型<text>*</text></view>
				<view class="item-right">{{ houseType_text }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">房主<text>*</text></view>
				<navigator class="item-right" url="../../Public/People" hover-class="none">{{ houseHolderId_text }}</navigator>
			</view>
			<view class="add-item" @click="show=true">
				<view class="item-left">楼层<text>*</text></view>
				<view class="item-right">{{ floorId_text }}</view>
			</view>
			
			<view class="add-item">
				<view class="item-left">描述</view>
				<view class="item-right"><textarea v-model="synopsis" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入描述"></textarea></view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 房屋类型 -->
		<u-select v-model="dictListShow" :list="dictList" @confirm="confirmType"></u-select>
		<u-select v-model="show" mode="single-column" :list="floorList" value-name="id" label-name="fullName" @confirm="confirm"></u-select>
		
	</view>
</template>

<script>
	export default{
		data() {
			return{
				houseId:'',
				show:false,
				dictListShow:false,
				dictList:[],
				avator:'', // 图片
				name:'', // 名称
				synopsis:'', // 描述
				houseType:'', // 房屋类型
				houseType_text:'请选择',
				houseRegisterCode:'',// 房屋注册号
				houseHolderId:'', // 房屋主ID
				houseHolderId_text:'请选择',
				floorId:'', // 楼层ID
				floorId_text:'请选择',
				floorList:[]
			}
		},
		onLoad(options) {
			this.houseId = options.id
		},
		mounted() {
			this.getDictionaries()
			this.getFloor()
			this.getHouseById()
		},
		onShow() {
			if(!uni.getStorageSync('people')) return
			let houseUser = uni.getStorageSync('people')
			this.houseHolderId_text = houseUser.name
			this.houseHolderId = houseUser.id
		},
		methods:{
			// 根据ID获取房屋
			getHouseById() {
				let params = {
					houseId:this.houseId
				}
				this.$u.api.getHouseById(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							this.avator = res.result.houseImgs
							this.name = res.result.name
							this.synopsis = res.result.synopsis
							this.houseType = res.result.houseType
							this.houseHolderId = res.result.houseHolderId
							this.houseHolderId_text = res.result.houseHolderName
							this.houseRegisterCode = res.result.houseRegisterCode
							this.floorId = res.result.floorId
							this.floorId_text = res.result.floorName
							this.dictList.forEach(item => {
								if(item.value == res.result.houseType) {
									this.houseType_text = item.label
								}
							})
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'获取房屋失败',
							icon:'none'
						})
					})
			},
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.avator = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 户籍类型
			getDictionaries() {
				let params = {
					dictCode:'house_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.dictList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 确认户籍
			confirmType(e) {
				// console.log(e)
				this.houseType_text = e[0].label
				this.houseType = e[0].value
			},
			//楼层
			getFloor() {
				this.$u.api.getFloor()
					.then(res => {
						if(res.code == 200) {
							// console.log(res)
							this.floorList = res.result
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'获取楼层失败'
						})
					})
			},
			// 确认楼层
			confirm(e) {
				// console.log(e)
				this.floorId = e[0].value
				this.floorId_text = e[0].label
			},
			// 选择地址
			chooseAddress() {
				let self = this
				uni.chooseLocation({
					success(res) {
						console.log(res)
						self.domicile.address = res.address
						self.domicile.longitude = res.longitude
						self.domicile.latitude = res.latitude
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'获取地址失败'
						})
					}
				})
			},
			// 提交 添加户籍
			handleSubmit() {
				if(this.avator == '') {
					return this.$refs.uToast.show({
						title: '请上传图片'
					})
				}
				if(this.name == '') {
					return this.$refs.uToast.show({
						title: '请填写名称'
					})
				}
				if(this.houseType == '') {
					return this.$refs.uToast.show({
						title: '请选择房屋类型'
					})
				}
				if(this.houseRegisterCode == '') {
					return this.$refs.uToast.show({
						title: '请填写房屋注册号'
					})
				}
				if(this.houseHolderId == '') {
					return this.$refs.uToast.show({
						title: '请选择房主'
					})
				}
				if(this.floorId == '') {
					return this.$refs.uToast.show({
						title: '请选择楼层'
					})
				}
				let params = {
					images:this.avator,
					name:this.name,
					synopsis:this.synopsis,
					houseType:this.houseType,
					houseRegisterCode:this.houseRegisterCode,
					houseHolderId:this.houseHolderId,
					floorId:this.floorId,
					id:this.houseId
				}
				console.log(params)
				uni.removeStorageSync('people')
				this.$u.api.editHouse(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'房屋修改成功',
								icon:'success'
							})
							this.avator = ''
							this.name = ''
							this.synopsis = ''
							this.houseType = ''
							this.houseType_text = '请选择'
							this.houseRegisterCode = ''
							this.houseHolderId = ''
							this.houseHolderId_text = '请选择'
							this.floorId = ''
							this.floorId_text = '请选择'
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'房屋修改失败',
							icon:'success'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text-align: left;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					flex: 6;
					font-size: 27rpx;
					color: #999;
					text-align: right;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #cecece;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
